package cn.neusoft.dao.impl;

import cn.neusoft.bean.Cart;
import cn.neusoft.bean.Deliveryaddress;
import cn.neusoft.dao.IDeliveryaddressDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.DeliveryaddressQuery;
import cn.neusoft.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DeliveryaddressDao implements IDeliveryaddressDao {
    private QueryRunner queryRunner;

    public DeliveryaddressDao(){
        queryRunner = JdbcUtils.getQueryRunner();
    }

    @Override
    public int add(Deliveryaddress deliveryaddress) throws SQLException {
        String sql="insert into t_deliveryaddress (contactName, contactSex, contactTel, address, userId)  values(?,?,?,?,?)";
        return queryRunner.update(sql,
                deliveryaddress.getContactName(),
                deliveryaddress.getContactSex(),
                deliveryaddress.getContactTel(),
                deliveryaddress.getAddress(),
                deliveryaddress.getUserId());
    }

    @Override
    public int update(Deliveryaddress deliveryaddress) throws SQLException {
        String sql="update t_deliveryaddress set contactName=?, contactSex=?, contactTel=?, address=?, userId=? where daId=?";
        return queryRunner.update(sql,
                deliveryaddress.getContactName(),
                deliveryaddress.getContactSex(),
                deliveryaddress.getContactTel(),
                deliveryaddress.getAddress(),
                deliveryaddress.getUserId(),
                deliveryaddress.getDaId());
    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql="delete from t_deliveryaddress where daId=?";
        return queryRunner.update(sql,id);
    }

    @Override
    public Deliveryaddress getById(Integer id) throws SQLException {
        String sql="select daId,contactName, contactSex, contactTel, address, userId from t_deliveryaddress where daId=?";
        return queryRunner.query(sql, new BeanHandler<Deliveryaddress>(Deliveryaddress.class), id);
    }

    @Override
    public List<Deliveryaddress> getAll() throws SQLException {
        String sql="select daId,contactName, contactSex, contactTel, address, userId from t_deliveryaddress";
        return queryRunner.query(sql, new BeanListHandler<Deliveryaddress>(Deliveryaddress.class));

    }

    @Override
    public int getCount(BaseQuery baseQuery) throws SQLException {
        DeliveryaddressQuery deliveryaddressQuery=(DeliveryaddressQuery)baseQuery;
        List<Object> params = new ArrayList<>();

        String sql="select count(daId) from t_deliveryaddress where 1=1 ";
        if(deliveryaddressQuery.getContactName()!=null){
            sql+=" and contactName like concat('%',?,'%')";
            params.add(deliveryaddressQuery.getContactName());
        }
        return queryRunner.query(sql,new ScalarHandler<Long>(),params.toArray()).intValue();
    }

    @Override
    public List<Deliveryaddress> getByPage(BaseQuery baseQuery) throws SQLException {
        DeliveryaddressQuery deliveryaddressQuery=(DeliveryaddressQuery)baseQuery;
        List<Object> params = new ArrayList<>();

        String sql="select daId,contactName, contactSex, contactTel, address, userId from t_deliveryaddress where 1=1 ";
        if(deliveryaddressQuery.getContactName()!=null){
            sql+=" and contactName like concat('%',?,'%')";
            params.add(deliveryaddressQuery.getContactName());
        }
        sql+=" order by daId desc limit ?,?";

        params.add(deliveryaddressQuery.getStartIndex());
        params.add(deliveryaddressQuery.getPageCount());
        return queryRunner.query(sql,new BeanListHandler<Deliveryaddress>(Deliveryaddress.class),params.toArray());
    }
}
